
! Global declarations and constants
module globals

  implicit none
  
  integer, parameter :: NI=502  ! total grid number
  ! this should be read-in and used for allocatation but ok for now

  real(8), dimension(NI) :: r
  real(8), dimension(NI) :: Tw
  real(8), dimension(NI) :: Qr

  integer :: maxstp, tstep, tpstep, nchi, mflag1, mflag2, eflag1, eflag2, eflag3, bflag
  integer :: etot, etot_old, detot
  real(8) :: dt, time, timep, tmax, dtp, dtg, dtl, rep, nrep
  real(8) :: dr, rad, As, Ac
  real(8) :: heatLoss,heatIn, Qw_p, Te_last, Ta_last, Tw_last, Tw_check
  real(8) :: Epulse,Aeff,Qabs,Et, E_total
  real(8) :: Ce,Ca,Vp,Emelt,Ereq
  real(8) :: Te,Ta,Tenew,Tanew,Qw, intCond, Ta_old
  real(8) :: length, ls, xc, tau, Fl
  real(8) :: chi, Qtot, Efield, Ao, CC, Fo
  real(8), allocatable :: Qnf(:,:), rchi(:), rf(:)
  
  !--constants
  real(8) ::    pi = 3.1415926535897932380d0
  real(8) :: 	rho = 1000.0d0                       ! water density kg/m3
  real(8) :: 	cp = 4176.0d0                        ! water specific heat J/kgK
  real(8) :: 	cond = 0.58d0!0.620d0                ! water conductivity W/mK
  real(8) :: 	intCondo = 105.0d6                   ! interface conductance W/m2K
  real(8) :: 	Tamb=300.0d0                         ! ambient temp
  real(8) :: 	Tini=300.0d0                         ! initial temp
  real(8) :: 	Geph=2.0d16                          ! in W/m3K
  real(8) ::    wvratio=30.0d0                       ! k ratio of liquid to vapor water
  real(8) ::    Avagadro=6.0221415d23                ! avagadro's number
  real(8) ::    molw=18.01528d0                      ! molecular weight water (g/mol)
  
  !--emission related
  real(8) :: therm, mpe
  real(8) :: ftherm, ftherm_old
  real(8) :: fmpe, fmpe_old
  real(8) :: c_therm, c_mpe
  real(8) :: toffset
  
  !--emission constants
  real(8) ::    bh=4.25d0                  ! eV, barrier height Au
  real(8) ::    w=780.0d-9                 ! wavelength of light
  real(8) ::    h=6.626d-34                ! Planck's
  real(8) ::    Ae=70.0d0                  ! Ce constant Au
  real(8) ::    eo=8.854d-12               ! permittivity of free space
  real(8) ::    c=3.0D8                    ! speed of light
  real(8) ::    kb=1.38d-23                ! Boltzmann m^2kg/(s^2K)
  real(8) ::    eVJ=1.602d-19              ! 1ev=eVJ J
  real(8) ::    ec=1.602d-19               ! electron charge C
  real(8) ::    latspace=0.4d-9            ! gold lattice spacing
  real(8) ::    ord=3.0d0                  ! mp emission order
  real(8) ::    Ecrit=2.7d10               ! monolayer ablation
  real(8) ::    Tw_spin=467.0d0            ! explosive boiling
  real(8) ::    Efermi=5.5d0               ! Fermi energy of gold
  real(8) ::    me=9.109D-31               ! electron mass (kg)
  
  !--file names and paths
  character (len=100) :: temp_out !="temperature.txt"  ! temperature result file
  character (len=100) :: dom_out="domain.txt"        ! water domain results file
  character (len=100) :: ep_out="energy.txt"         ! laser pulse file
  character (len=100) :: fo_in="fowler.txt"          ! fowler function data file
  character (len=100) :: para_in="ns_param.txt"      ! simulation parameter file
!  character (len=100) :: nf_in="poy_50_780.txt"      ! near-field enhancement file
  character (len=100) :: nf_in="poynting_dan_perp.txt" ! near-field enhancement file
!  character (len=100) :: nf_in="poynting_50_780.txt" ! near-field enhancement file
  character (len=100) :: opath="../results/"         ! relative output(results) directory
  character (len=100) :: ipath="../inputs/"          ! relative input directory
  	  
  character :: jk
  logical   :: erun, mrun, brun, efficient,eoutrun
  integer   :: Te_flag, Tw_flag, Ta_flag
  
contains
!--------------------------------------------------------------------------

  subroutine read_param
  
    implicit none
  
    open(11,file=trim(adjustl(ipath))//trim(adjustl(para_in)),form='FORMATTED',status='old')     
    read(11,*) rad,   jk
    read(11,*) Qabs,  jk
    read(11,*) tau,   jk
    read(11,*) Fl,    jk
    read(11,*) tmax,  jk
    read(11,*) erun,  jk
    read(11,*) eoutrun,  jk
    read(11,*) mrun,  jk
    read(11,*) brun,  jk
    read(11,*) efficient, jk
    read(11,*) rep,   jk
    read(11,*) temp_out,   jk
    close(11)
	
	As = 4.0D0*pi*(rad**2.0D0)             ! physical surface area
	Ac = pi*(rad**2.0D0)                   ! physical cross-sectional area
	Vp = 4.0D0*pi*(rad**3.0D0)/3.0D0       ! physical particle volume
	Ereq = Vp*19300.0d0*64.0d3       ! required energy for complete melting in J, DHl*Vp*rho
	Epulse = Fl*Ac*Qabs              ! integrated energy per pulse

	write(*,*) " "
	write(*,*) "     Simulation Parameters (in J and m)"
	write(*,*) "================================================="
	write(*,'(a,6x,d12.4)') " Particle radius: ", rad
	write(*,'(a,f6.3)') " Absoroption efficiency: ", Qabs
	write(*,'(a,2x,d12.4)') " FWHM Pulse duration: ", tau
	write(*,'(a,d12.4)') " Applied pulse fluence: ", Fl
	write(*,'(a,d12.4)') " Absorbed pulse energy: ", Epulse
	if (rep .gt. 0.0) then
	   write(*,'(a,f6.2)') " Repetition rate (MHz): ", rep
	else
	   write(*,*) " Single pulse simulation"
	endif
	if (erun) then
	   write(*,*) " Electron emission will be calculated"
	else
	   write(*,*) " Electron emission will not be calculated"
	endif
	if (eoutrun) then
	   write(*,*) " Electron emission will be considered for heating"
	else
	   write(*,*) " Electron emission will not be considered for heating"
	endif
	if (mrun) then
	   write(*,*) " Particle melting is considered"
	else
	   write(*,*) " Particle melting is not considered"
	endif
	if (brun) then
	   write(*,*) " Water boiling is considered"
	else
	   write(*,*) " Water boiling is not considered"
	endif
	write(*,*) "================================================="
	write(*,*) " "
    if (efficient) then
	   write(*,*) " Simulation will be aborted when water reaches a maximum temperature"
	endif
	write(*,*) " "

  end subroutine read_param
!--------------------------------------------------------------------------

end module globals
